package com.topnav.mc.base;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.js.lib.annotation.ApiLogger;
import com.js.lib.result.Result;
import com.topnav.mc.uitls.QueryPage;
import com.topnav.mc.validator.ValidList;
import com.topnav.mc.validator.group.IAdd;
import com.topnav.mc.validator.group.IDelete;
import com.topnav.mc.validator.group.ISelect;
import com.topnav.mc.validator.group.IUpdate;
import io.swagger.annotations.ApiOperation;
/**
 * 说明：
 * @类名: CurdController
 * <p>
 *
 * </p>
 * @author   kenny
 * @Date	 2021年11月19日上午10:44:05
 */
public abstract class CurdController<T,Service extends IService<T>> {
    @Autowired(required = false)
    protected Service baseService;

    @ApiLogger(apiName="/query/list")
    @GetMapping("/query/list")
    @ApiOperation(value = "查询所有")
    @ResponseBody
    public Result queryList() {
    	List datas = baseService.list();
        return Result.ok(datas);
    }

    /**
     * 批量新增
     */
    @ApiLogger(apiName="/adds")
    @ApiOperation(value = "批量新增")
    @PostMapping("/adds")
    @ResponseBody
    public Result adds(@Validated(IAdd.class) @RequestBody ValidList<T> datas) {
    	baseService.saveBatch(datas,datas.size());
    	return Result.ok();
    }


    /**
     * 新增
     */
    @ApiLogger(apiName="/add")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @ResponseBody
    public Result add(@Validated(IAdd.class) @RequestBody T data) {
    	baseService.save(data);
    	return Result.ok(data);
    }



    /**
     * 通过ID修改
     */
    @ApiLogger(apiName="/update")
    @ApiOperation(value = "通过ID修改")
    @PostMapping("/update")
    @ResponseBody
    public Result update(@Validated(IUpdate.class) @RequestBody T data) {
    	baseService.updateById(data);
    	return Result.ok();
    }


    /**
     * 删除
     */
    @ApiLogger(apiName="/delete")
    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@Validated(IDelete.class) @RequestBody T data) {
    	baseService.remove(new QueryWrapper<>(data));
        return Result.ok();
    }

    /**
     * 通过查询条件获取多条数据
     */
    @ApiLogger(apiName="/query/wherelist")
    @ApiOperation(value = "通过查询条件获取多条数据")
    @PostMapping("/query/wherelist")
    @ResponseBody
    public Result queryWhereList(@Validated(ISelect.class) @RequestBody T data) {
        List datas = baseService.list(new QueryWrapper<>(data));
        return Result.ok(datas);
    }

    /**
     * 分页查询
     * @param p
     * @return
     */
    @ApiLogger(apiName="/query/page")
    @ApiOperation(value = "带有条件的分页查询")
    @PostMapping("/query/page")
    @ResponseBody
    public Result queryPage(@Validated(IPage.class) @RequestBody QueryPage<T> p) {
    	Page datas = baseService.page(p.getPage(), new QueryWrapper<>(p.getWhere()));
        return Result.ok(datas);
    }

}
